package com.exposit.books.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.exposit.books.domain.Book;
import com.exposit.books.domain.Buyer;
import com.exposit.books.domain.Order;
import com.exposit.books.service.BookService;
import com.exposit.books.service.UserService;


@Controller
@RequestMapping("/profile")
public class AccountController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private BookService bookService;
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ModelAndView showUserProfile(
			@PathVariable String id,
			HttpSession session, Model model) {
		
		ModelAndView mav = new ModelAndView();
		Buyer buyer = (Buyer)session.getAttribute("user");
		if (buyer == null || buyer.getId() != Long.parseLong(id))
		{
			mav.setViewName("errorPage");
			return mav;
		}
		
		
		List<Book> books = userService.getAllBooks();
		mav.addObject("books", books);
		mav.setViewName("profile");		
		return mav;
	}
	

}
